package com.nbsaas.boot.product.rest.resource;

import com.nbsaas.boot.product.api.apis.ProductGroupApi;
import com.nbsaas.boot.product.data.entity.ProductGroup;
import com.nbsaas.boot.product.api.domain.request.ProductGroupRequest;
import com.nbsaas.boot.product.api.domain.response.ProductGroupResponse;
import com.nbsaas.boot.product.api.domain.simple.ProductGroupSimple;
import com.nbsaas.boot.product.rest.convert.ProductGroupSimpleConvert;
import com.nbsaas.boot.product.rest.convert.ProductGroupEntityConvert;
import com.nbsaas.boot.product.rest.convert.ProductGroupResponseConvert;
import com.nbsaas.boot.product.data.repository.ProductGroupRepository;

import java.io.Serializable;
import com.nbsaas.boot.jpa.data.core.BaseResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;

import java.util.function.Function;
import com.nbsaas.boot.rest.request.PageRequest;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.product.api.domain.request.ProductGroupSearch;
import com.nbsaas.boot.rest.utils.TreeUtils;
import java.util.List;
/**
*   商品分类-业务接口实现
*/
@Transactional
@Service
public class ProductGroupResource extends BaseResource<ProductGroup,ProductGroupResponse, ProductGroupSimple, ProductGroupRequest>  implements ProductGroupApi {

    @Resource
    private ProductGroupRepository productGroupRepository;

    @Override
    public JpaRepositoryImplementation<ProductGroup, Serializable> getJpaRepository() {
        return productGroupRepository;
    }

    @Override
    public Function<ProductGroup, ProductGroupSimple> getConvertSimple() {
        return new ProductGroupSimpleConvert();
    }

    @Override
    public Function<ProductGroupRequest, ProductGroup> getConvertForm() {
        return new ProductGroupEntityConvert();
    }

    @Override
    public Function<ProductGroup, ProductGroupResponse> getConvertResponse() {
        return new ProductGroupResponseConvert();
    }



    @Transactional(readOnly = true)
    @Override
    public ListResponse<ProductGroupSimple> list(PageRequest request) {
        ProductGroupSimpleConvert convert=new ProductGroupSimpleConvert();
        if (request instanceof ProductGroupSearch){
             ProductGroupSearch searchRequest=(ProductGroupSearch)request;
             convert.setFetch(searchRequest.getFetch());
        }
        return listSimple(request,convert);
    }


    @Transactional(readOnly = true)
    @Override
    public ListResponse<ProductGroupSimple> root(ProductGroupSearch search) {
        ListResponse<ProductGroupSimple> result=new  ListResponse<>();
        List<ProductGroupSimple> simples = list(search).getData();
        result.setData(TreeUtils.tree(simples));
        return result;
    }


}


